/*
 * 类文件名:  TimeUtils.java
 * 著作版权:  深圳市易商云电子商务有限公司 Copyright 2012-2022, E-mail: jinting_peng@esyto.com, All rights reserved
 * 功能描述:  <描述>
 * 类创建人:  jting
 * 创建时间:  2016-6-18
 * 功能版本:  V001Z0001
 */
package com.ruoyi.SuveryInterface.SuveryUtils.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 有关时间的工具类
 *
 * @author zhb
 * @version V001Z0001
 * @date 2017-2-15
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class TimeUtils {

	public static String getTimeFormat(String dateStr, String pattern) {
		Date date = dateStrToDate(dateStr, pattern);

		SimpleDateFormat format = new SimpleDateFormat(pattern);

		return format.format(date);
	}

	public static Date dateStrToDate(String dateStr, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		try {
			return format.parse(dateStr);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 根据日期取得星期几
	 *
	 * @author zhb
	 * @version V001Z0001
	 * @date 2017-2-15
	 * @see [相关类/方法]
	 * @since [产品/模块版本]
	 */
	public static String getWeek(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
		String week = sdf.format(date);
		return week;
	}

	public static String secondsFromDate(long seconds, String pattern) {
		Date date = new Date(seconds);
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		String dateStr = format.format(gc.getTime());

		return dateStr;
	}

	public static String formatTime(String timeStr, String srcPattern, String toPattern) {
		SimpleDateFormat format = new SimpleDateFormat(srcPattern);
		try {
			Date date = format.parse(timeStr);
			format = new SimpleDateFormat(toPattern);
			return format.format(date);
		} catch (ParseException e) {
			return "";
		}
	}

	public static String dateMatcher(String trans_date) {
		Pattern pattern = Pattern.compile("^\\d{4}-\\d{1,2}-\\d{1,2}$");
		Matcher matcher = pattern.matcher(trans_date);
		if (matcher.matches()) {
			return getTimeFormat(trans_date, "yyyy-MM-dd");
		}

		pattern = Pattern.compile("^\\d{4}-\\d{1,2}$");
		matcher = pattern.matcher(trans_date);
		if (matcher.matches()) {
			return getTimeFormat(trans_date, "yyyy-MM");
		}

		pattern = Pattern.compile("^\\d{4}$");
		matcher = pattern.matcher(trans_date);
		if (matcher.matches()) {
			return getTimeFormat(trans_date, "yyyy");
		}

		return getTimeFormat(trans_date, "yyyy-MM-dd");
	}

	/**
	 * 计算两个时间相隔的天数
	 *
	 * @author zhb
	 * @version V001Z0001
	 * @date 2017-2-15
	 * @see [相关类/方法]
	 * @since [产品/模块版本]
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 计算时间加上天数
	 *
	 * @author zhb
	 * @version V001Z0001
	 * @date 2017-2-15
	 * @see [相关类/方法]
	 * @since [产品/模块版本]
	 */
	public static Date moveDate(Date date, int time) throws ParseException {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, time);
		date = calendar.getTime();
		return date;
	}
	/**
	 * 计算时间差
	 *
	 * @param time
	 * @return
	 */
	public static String costTime(String time) {
		if (time != null) {
			long stratTime = Long.parseLong(time);
			long endTime = System.currentTimeMillis();
			int time1 = (int) stratTime;
			int time2 = (int) endTime;
			// 用新的字符编码生成字符串
			return Integer.toString(time2 - time1);
		}
		return time;
	}
	/**
	 * 计算时间差
	 *
	 * @param time
	 * @return
	 */
	public static String costTime(long stratTime) {
			int time1 = (int) stratTime;
			int time2 = (int) System.currentTimeMillis();
			// 用新的字符编码生成字符串
			return Integer.toString(time2 - time1);
	}
	/**
	 * 计算通道时间差
	 *
	 * @param time
	 * @return
	 */
	public static String costSupplierTime(long stratTime,long endTime) {
			int time1 = (int) stratTime;
			int time2 = (int) endTime;
			// 用新的字符编码生成字符串
			return Integer.toString(time2 - time1);
	}
	/**
	 * 获取当前时间年月
	 *
	 * @return
	 */
	public static String returnTime(){
		Calendar now = Calendar.getInstance();
		String year = String.valueOf(now.get(Calendar.YEAR));
		String month = String.valueOf((now.get(Calendar.MONTH) + 1));
		if(month.length()==1){
			month="0"+month;
		}
		return year+month;
	}
}
